package com.administrator.scoreManageSys.controller.course;

import com.administrator.scoreManageSys.service.ClassMangeService;
import com.administrator.scoreManageSys.service.TeachCourseService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;

/**
 * 授课管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/teach/course")
public class TeachCourseController {

    @Resource
    private TeachCourseService teachCourseService;

    /**
     * 获取授课信息列表
     *
     * @return 列表
     */
    @PostMapping(value = "/courseList")
    @RequiresRoles("education")
    @RequiresPermissions(value = "teachingManagement:list", logical = Logical.OR)
    public JSONObject courseList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            String teacherName = data.getString("teacherName");
            return teachCourseService.courseList(pageNum, pageSize, teacherName);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取授课信息列表 异常");
        }
    }

    /**
     * 添加授课信息
     *
     * @return 列表
     */
    @PostMapping(value = "/courseAdd")
    @RequiresRoles("education")
    @RequiresPermissions(value = "teachingManagement:add", logical = Logical.OR)
    public JSONObject courseAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Integer classId = data.getInteger("class");
            Integer subject = data.getInteger("subject");
            Integer teachUser = data.getInteger("teachUser");
            return teachCourseService.courseAdd(userId,classId,subject,teachUser);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加授课信息 异常");
        }
    }

    /**
     * 修改授课信息
     *
     * @return 列表
     */
    @PostMapping(value = "/courseUpdate")
    @RequiresRoles("education")
    @RequiresPermissions(value = "teachingManagement:edit", logical = Logical.OR)
    public JSONObject courseUpdate(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作id
            Integer id = data.getInteger("id");
            Integer classId = data.getInteger("class");
            Integer subject = data.getInteger("subject");
            Integer teachUser = data.getInteger("teachUser");
            return teachCourseService.courseUpdate(id,classId,subject,teachUser);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改授课信息 异常");
        }
    }

    /**
     * 删除授课信息
     *
     * @return 列表
     */
    @PostMapping(value = "/courseDelete")
    @RequiresRoles("education")
    @RequiresPermissions(value = "teachingManagement:add", logical = Logical.OR)
    public JSONObject courseDelete(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作id
            Integer id = data.getInteger("id");
            return teachCourseService.courseDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除授课信息 异常");
        }
    }

    /**
     * 获取考试管理列表
     *
     * @return 列表
     */
    @PostMapping(value = "/examList")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:list", logical = Logical.OR)
    public JSONObject examList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            Integer season = data.getInteger("season");
            Integer subject = data.getInteger("subject");
            String name = data.getString("name");
            return teachCourseService.examList(pageNum, pageSize, season,subject,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取授课信息列表 异常");
        }
    }

    /**
     * 添加考试管理
     *
     * @return 列表
     */
    @PostMapping(value = "/examAdd")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:add", logical = Logical.OR)
    public JSONObject examAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Integer season = data.getInteger("season");
            Integer classId = data.getInteger("class");
            Integer subject = data.getInteger("subject");
            String name = data.getString("name");
            return teachCourseService.examAdd(season,classId,subject,name,userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加考试管理 异常");
        }
    }

    /**
     * 修改考试管理
     *
     * @return 列表
     */
    @PostMapping(value = "/examUpdate")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:edit", logical = Logical.OR)
    public JSONObject examUpdate(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作id
            Integer id = data.getInteger("id");
            Integer season = data.getInteger("season");
            Integer classId = data.getInteger("class");
            Integer subject = data.getInteger("subject");
            String name = data.getString("name");
            return teachCourseService.examUpdate(id,classId,subject,season,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改考试管理 异常");
        }
    }

    /**
     * 删除考试管理
     *
     * @return 列表
     */
    @PostMapping(value = "/examDelete")
    @RequiresRoles("education")
    @RequiresPermissions(value = "examination:add", logical = Logical.OR)
    public JSONObject examDelete(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作id
            Integer id = data.getInteger("id");
            return teachCourseService.examDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除考试管理信息 异常");
        }
    }

    /**
     * 添加任课教师
     *
     * @return 列表
     */
    @PostMapping(value = "/theTeacherAdd")
    @RequiresRoles(value = {"teacherManagement"})
    @RequiresPermissions(value = {"teacherPost:add"}, logical = Logical.OR)
    public JSONObject theTeacherAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            //操作id
            Integer teacherId = data.getInteger("teacherId");
            Integer subject = data.getInteger("subject");
            return teachCourseService.theTeacherAdd(teacherId,subject,userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加任课教师 异常");
        }
    }
    /**
     * 获取任课教师
     *
     * @return 列表
     */
    @PostMapping(value = "/theTeacherList")
    @RequiresRoles(value = {"teacherManagement"})
    @RequiresPermissions(value = {"teacherPost:list"}, logical = Logical.OR)
    public JSONObject theTeacherList(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            Integer subject = data.getInteger("subject");
            String name = data.getString("name");
            return teachCourseService.theTeacherList(pageNum,pageSize,subject,name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取任课教师 异常");
        }
    }

    /**
     * 修改任课教师
     *
     * @return 列表
     */
    @PostMapping(value = "/theTeacherUpdate")
    @RequiresRoles(value = {"teacherManagement"})
    @RequiresPermissions(value = {"teacherPost:add"}, logical = Logical.OR)
    public JSONObject theTeacherUpdate(@RequestBody JSONObject data, HttpServletRequest request) {
        try {

            //修改id
            Integer id = data.getInteger("id");
            Integer teacherId = data.getInteger("teacherId");
            Integer subject = data.getInteger("subject");
            return teachCourseService.theTeacherUpdate(teacherId,subject,id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改任课教师 异常");
        }
    }
    /**
     * 删除任课教师
     *
     * @return 列表
     */
    @PostMapping(value = "/theTeacherDelete")
    @RequiresRoles(value = {"teacherManagement"})
    @RequiresPermissions(value = {"teacherPost:add"}, logical = Logical.OR)
    public JSONObject theTeacherDelete(@RequestBody JSONObject data, HttpServletRequest request) {
        try {

            //修改id
            Integer id = data.getInteger("id");
            return teachCourseService.theTeacherDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除任课教师 异常");
        }
    }
}
